/*
 * 
 * 
 * 
 */
package com.hboxs.asl.service.impl;

import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.dao.MessageDao;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.Message;
import com.hboxs.asl.service.MessageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Service - 消息
 */
@Service("messageServiceImpl")
public class MessageServiceImpl extends BaseServiceImpl<Message, Long> implements MessageService {

    @Resource(name = "messageDaoImpl")
    private MessageDao messageDao;

    @Resource(name = "messageDaoImpl")
    public void setBaseDao(MessageDao messageDao) {
        super.setBaseDao(messageDao);
    }

    @Transactional(readOnly = true)
    public Page<Message> findPage(Member member, Pageable pageable) {
        return messageDao.findPage(member, pageable);
    }

    @Transactional(readOnly = true)
    public Page<Message> findDraftPage(Member sender, Pageable pageable) {
        return messageDao.findDraftPage(sender, pageable);
    }

    @Transactional(readOnly = true)
    public Long count(Member member, Boolean read) {
        return messageDao.count(member, read);
    }

    public void delete(Long id, Member member) {
        messageDao.remove(id, member);
    }

    @Override
    public List<Message> findByMember(Member member) {
        return messageDao.findByMember(member);
    }

    @Override
    public void addMessage(Member member, String title, String content) {
        Message m = new Message();
        m.setReceiver(member);
        m.setTitle(title);
        m.setContent(content);
        m.setIp("1.1.1.1");
        m.setIsDraft(false);
        m.setReceiverDelete(false);
        m.setReceiverRead(false);
        m.setSenderDelete(false);
        m.setSenderRead(true);
        save(m);
    }

    @Override
    public void generateMessage(Member sender, Member receiver, String information) {
        Message message = new Message();
        message.setId(null);
        message.setContent(information);
        message.setMosaicContent(information);
        message.setSender(sender);
        message.setReceiver(receiver);
        message.setTitle("易经应用");
        this.save(message);
    }

}